package cn.lsh.reducer;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.Text;

import java.io.IOException;

public class HbaseMaxReduce extends TableReducer<Text, Text, ImmutableBytesWritable> {

	@Override
	protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
		int max = 0;
		String col = "";
		for (Text value : values) {
			String[] ss = StringUtils.split(value.toString(), ":");
			int count = Integer.parseInt(ss[1]);
			if (count > max) {
				max = count;
				col = ss[0];
			}
		}
		Put put = new Put(Bytes.toBytes("max123"));
		put.addColumn(Bytes.toBytes("max"), Bytes.toBytes(col), Bytes.toBytes(max + ""));
		context.write(null, put);
	}

}
